package com.uchengwang.ui.utils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.IdRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.adapter.MoreAdapter;
import com.uchengwang.ui.base_ui.bean.MoreBean;
import com.uchengwang.ui.cm_ui.cycle.adapter.BottomAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.BottomBean;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.umsg.adapter.MoreConditionAdapter;
import com.uchengwang.ui.cm_ui.umsg.bean.MoreConditionBean;
import com.uchengwang.ui.cm_ui.umsg.bean.MoreConditionListBean;
import com.uchengwang.ui.cm_ui.umsg.bean.MoreConditionResultBean;
import com.uchengwang.ui.cm_ui.uu.adapter.ChooseItemAdapter;
import com.uchengwang.ui.cm_ui.uu.adapter.ChooseItemIconAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.ChooseItemBean;
import com.uchengwang.ui.view.listener.MoreConditionResultListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2016/4/26.
 * 动态添加item 从底部弹出
 */
public class PopupUtils {
    private static PopupUtils utils;
    private PopupWindow mPopupWindow;
    private PopupOnItemListener listener;
    private PopupUtils (){}
    private boolean isFouce = true;//popupwindow焦点


    public static PopupUtils getUtils(){
        if (utils == null){
            utils = new PopupUtils();
        }
        return utils;
    }


    /**
     * 按条件找人
     * @param isWomen 上次选择的是不是性别为女
     * @param isSameCity 上次选择的条件是不是同城
     * @param y 上次选择的入学年份
     * */
    private String sexType = "", sameType, yearType;
    public PopupUtils selectPerson(final Context context, boolean isWomen, boolean isSameCity, String y, final PopupConditionListener listener){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_find_condition, null, false);
        final RadioGroup sex = (RadioGroup) view.findViewById(R.id.condition_sex);
        final RadioGroup same = (RadioGroup) view.findViewById(R.id.condition_same);
        final RadioGroup year = (RadioGroup) view.findViewById(R.id.condition_year);

        AutoUtils.autoSize(view);

        if (isWomen){
            sex.check(R.id.condition_women);
            sexType = "女";
        }else {
            sex.check(R.id.condition_men);
            sexType = "男";
        }

        if (isSameCity){
            same.check(R.id.condition_same_city);
            sameType = "同城";
        }else {
            same.check(R.id.condition_same_school);
            sameType = "本校";
        }

        if (!TextUtils.isEmpty(y)){
            switch (y){
                case "2014":
                    year.check(R.id.condition_year_2014);
                    yearType = "2014";
                    break;
                case "2015":
                    year.check(R.id.condition_year_2015);
                    yearType = "2015";
                    break;
                case "2016":
                    year.check(R.id.condition_year_2016);
                    yearType = "2016";
                    break;
                case "2017":
                    year.check(R.id.condition_year_2017);
                    yearType = "2017";
                    break;
            }

        }


        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                RadioButton button = (RadioButton) sex.findViewById(i);
                sexType = button.getText().toString();
            }
        });

        same.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                RadioButton button = (RadioButton) same.findViewById(i);
                sameType = button.getText().toString();
            }
        });

        year.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                RadioButton button = (RadioButton) year.findViewById(i);
                yearType = button.getText().toString();
                List<String> strings = new ArrayList<String>();
                strings.add(sexType);
                strings.add(sameType);
                strings.add(yearType);
                listener.onResult(strings);

                dismiss();
            }
        });

        init(context, view, false, true);
        return this;
    }


    /**
     * 发布退出保存提示
     * */
    public PopupUtils initRelease(Context context, View shoeView){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_item_release, null, false);
        TextView save = (TextView) view.findViewById(R.id.layout_save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onClick(false, "保存");
                }
                dismiss();
            }
        });
        TextView noSave = (TextView) view.findViewById(R.id.layout_noSave);
        noSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onClick(false, "不保存");
                }
                dismiss();
            }
        });
        TextView cacel = (TextView) view.findViewById(R.id.layout_cancel);
        cacel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onClick(false, "取消");
                }
                dismiss();
            }
        });

        init(context, view, true, true);
        showBottom(context, shoeView);

        return utils;
    }

    /**
     * 底部选择操作
     * @param isCancelShow 是否显示底部的取消
     * */
    public PopupUtils bottom(Context context, List<BottomBean> list, boolean isCancelShow, View shoeView, AdapterView.OnItemClickListener listener){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_bottom_pupop, null, false);
        ListView lv = (ListView) view.findViewById(R.id.layout_lv);
        TextView cancel = (TextView) view.findViewById(R.id.layout_cancel);
        if (!isCancelShow){
            cancel.setVisibility(View.GONE);
        }else {
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        BottomAdapter adapter = new BottomAdapter(context, list);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(listener);

        init(context, view, true, true);
        showBottom(context, shoeView);

        return utils;
    }


    /**
     * 更多操作,显示在底部
     * */
    public void bottomMore(Context context, View showView, List<MoreBean> moreList, OnItemPopupClickListener listener){
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_more_layout, null, false);
        RecyclerView rv = (RecyclerView) view.findViewById(R.id.more_rv);
        TextView more_cancel = (TextView) view.findViewById(R.id.more_cancel);
        more_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        MoreAdapter adapter = new MoreAdapter(context, moreList);
        adapter.setListener(listener);
        rv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        rv.setAdapter(adapter);

        init(context, view, false, true);
        showBottom(context, showView);

    }

    /**
     * 更多操作,显示右上角
     * */
    public void rgihtMore(Context context, View showView, List<String> stringList, AdapterView.OnItemClickListener listener){
        View view = LayoutInflater.from(context).inflate(R.layout.right_more_layout, null, false);
        ListView listView = view.findViewById(R.id.choose_lv);

        ChooseItemAdapter adapter = new ChooseItemAdapter(context, stringList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(listener);

        init2(context, 261, view, false, true);
        showRight(context, showView);

    }
    /**
     * 更多操作,显示右上角 带上图片标签
     * */
    public void rgihtMoreWithIcon(Context context, View showView, List<ChooseItemBean> list, int listBg,  AdapterView.OnItemClickListener listener){
        View view = LayoutInflater.from(context).inflate(R.layout.right_more_layout, null, false);
        ListView listView = view.findViewById(R.id.choose_lv);
        if (listBg >0){
            listView.setBackgroundResource(listBg);
        }

        ChooseItemIconAdapter adapter = new ChooseItemIconAdapter(context, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(listener);

        init2(context, 261, view, false, true);
        showRight(context, showView);

    }

    /**
     * 多条件选择
     * */
    public void moreCondition(Context context, final List<MoreConditionListBean> listBeen , View showView, final MoreConditionResultListener listener){
        if (listBeen == null){
            return;
        }
        int size = listBeen.size();
        View baseView = LayoutInflater.from(context).inflate(R.layout.view_more_condition, null, false);
        LinearLayout contenView = (LinearLayout) baseView.findViewById(R.id.moreCondition_content);
        Button fast_ok = (Button) baseView.findViewById(R.id.moreCondition_ok);
        fast_ok.setVisibility(View.VISIBLE);
        fast_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<MoreConditionResultBean> resultBeanList = new ArrayList<MoreConditionResultBean>();
                for (MoreConditionListBean b : listBeen){
                    List<MoreConditionBean> list = b.getList();
                    for (MoreConditionBean conditionBean : list){
                        if (conditionBean.isCheck()){
                            resultBeanList.add(new MoreConditionResultBean(conditionBean.getTitle(), list.indexOf(conditionBean), conditionBean.getNum()));
                        }
                    }
                }
                dismiss();
                listener.onResult(resultBeanList);
            }
        });


        for (int i =0; i<size; i++){
            View itemView = LayoutInflater.from(context).inflate(R.layout.view_more_condition_view, null, false);

            MoreConditionListBean bean = listBeen.get(i);

            TextView tv = (TextView) itemView.findViewById(R.id.moreCondition_tv);
            tv.setText(bean.getTitle());

            RecyclerView rv = (RecyclerView) itemView.findViewById(R.id.moreCondition_rv);
            rv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            MoreConditionAdapter adapter = new MoreConditionAdapter(context, bean.getList());
            rv.setAdapter(adapter);

            contenView.addView(itemView);
        }

        init(context, baseView, false, true);
        show(context, showView);
    }


    /**
     * 投票的底部弹出
     * */
    public void voteBottom(final Context context, View showView, final UserBean userBean, final OnHttpResultSingleListener<UserBean> listener){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_vote_desc, null);
        LinearLayout desc_base = view.findViewById(R.id.desc_base);
        ImageView desc_head = view.findViewById(R.id.desc_head);
        final EditText name = view.findViewById(R.id.desc_name);
        final EditText desc_desc = view.findViewById(R.id.desc_desc);
        Button desc_sure = view.findViewById(R.id.desc_sure);
        if (userBean != null){
            ImageManager.getInstance().loadImage(context, desc_head, userBean.getUserAvatar(), ImageManager.ImageType.ROUND);
        }
        name.setText(userBean.getNickName());
        desc_desc.setText(userBean.getDec());

        desc_base.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UCommonUtil.getUtil().hideSoftInput(context, name);
            }
        });

        desc_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nameS = name.getText().toString();
                String descS = desc_desc.getText().toString();
                userBean.setNickName(nameS);
                userBean.setDec(descS);
                if (TextUtils.isEmpty(nameS)){
                    UCommonUtil.getUtil().showToast(context, "请完善投票信息");
                    return;
                }
                dismiss();
                listener.onResult(0, null, userBean);

            }
        });


        init(context, view, false, false);
        showBottom(context, showView);
    }


    /**
     *显示View
     * @param isAnim 是否需要动画
     * @param isCancel 是否点击外面可以取消
     */
    public PopupUtils showView(Context context, View showView, boolean isAnim, boolean isCancel, boolean isFouce){
        this.isFouce = isFouce;
        init(context, showView, isAnim, isCancel);

        return this;
    }


    private void init(final Context context, View view, boolean isAnim, boolean isCancel) {
        mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.WRAP_CONTENT, isFouce);
        init(context, isAnim, isCancel);
    }
    private void init2(final Context context, int width,  View view, boolean isAnim, boolean isCancel) {
        mPopupWindow = new PopupWindow(view, AutoUtils.getPercentWidthSize(width), ViewGroup.LayoutParams.WRAP_CONTENT, isFouce);
        init(context, isAnim, isCancel);
    }

    private void init(final Context context,  boolean isAnim, boolean isCancel){
        mPopupWindow.setOutsideTouchable(isCancel);
        if (isAnim){
            mPopupWindow.setAnimationStyle(R.style.AnimBottom);
        }
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(00000000));
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (listener != null) {
                    listener.onDismiss();
                }
                UCommonUtil.getUtil().backgroundAlpha(context, 1f);
                dismiss();
            }
        });
    }

    /**
     * 显示的位置
     * */
    public PopupUtils show(Context context, View view) {
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            UCommonUtil.getUtil().backgroundAlpha(context, 0.5f);
            mPopupWindow.showAsDropDown(view);
        }
        return this;
    }

    /**
     * 显示的位置 背景不会变色
     * */
    public PopupUtils showNo(View view) {
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            mPopupWindow.showAsDropDown(view);
        }
        return this;
    }

    /**
     * 显示在底部
     * */
    private PopupUtils showBottom(Context context, View view) {
        if (UCommonUtil.getUtil().isShowSoftInput(context)) {
            UCommonUtil.getUtil().hideSoftInput(context, view);
        }
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            UCommonUtil.getUtil().backgroundAlpha(context, 0.5f);
            mPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
        return this;
    }

    /**
     * 显示在右上部
     * */
    private PopupUtils showRight(Context context, View view) {
        if (UCommonUtil.getUtil().isShowSoftInput(context)) {
            UCommonUtil.getUtil().hideSoftInput(context, view);
        }
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            UCommonUtil.getUtil().backgroundAlpha(context, 0.5f);
            mPopupWindow.showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 30, view.getHeight() + 50);
        }
        return this;
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
        if (utils != null){
            utils = null;
        }
    }

    public boolean isShow(){
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            return mPopupWindow.isShowing();
        }
        return false;
    }

    public PopupUtils setClickListener(PopupOnItemListener listener) {
        this.listener = listener;
        return this;
    }


    public interface PopupOnItemListener {
        /**
         * isTop是否是现在本校或者同城 否则是底部的选择
         */
        void onClick(boolean isTop, String type);
        void onDismiss();
    }

    public interface OnItemPopupClickListener{
        void onItemClick(int index, String title);
    }

    /**
     * 按条件找人 将选择条件结果返回
     * */
    public interface PopupConditionListener{
        void onResult(List<String> list);
    }

}
